package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.qo.AppointmentQueryObject;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;


    @Override
    public TablePageInfo<Appointment> query(AppointmentQueryObject qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Appointment>(appointmentMapper.selectForList(qo));
    }


    @Override
    public void save(Appointment appointment) {
        Appointment appointment1 = new Appointment();

        appointment1.setCustomerName(appointment.getCustomerName());
        appointment1.setCustomerPhone(appointment.getCustomerPhone());
        appointment1.setAppointmentTime(appointment.getAppointmentTime());
        appointment1.setLicensePlate(appointment.getLicensePlate());
        appointment1.setCarSeries(appointment.getCarSeries());
        appointment1.setServiceType(appointment.getServiceType());
        appointment1.setInfo(appointment.getInfo());
        appointment1.setCreateTime(new Date());

        appointmentMapper.insert(appointment1);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectOne(id);
    }


    @Override
    public void update(Appointment appointment) {
        Appointment appointment1 = this.get(appointment.getId());
        if (!Appointment.STATUS_APPOINTMENT.equals(appointment1.getStatus())) {
            throw new BusinessException("异常操作");
        }

        appointment1.setCustomerName(appointment.getCustomerName());
        appointment1.setCustomerPhone(appointment.getCustomerPhone());
        appointment1.setAppointmentTime(appointment.getAppointmentTime());
        appointment1.setLicensePlate(appointment.getLicensePlate());
        appointment1.setCarSeries(appointment.getCarSeries());
        appointment1.setServiceType(appointment.getServiceType());
        appointment1.setInfo(appointment.getInfo());

        appointmentMapper.update(appointment1);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            appointmentMapper.delete(dictId);
        }
    }

    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }


    @Override
    public void cancel(Long id) {
        Appointment appointment = this.get(id);
        if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())) {
            throw new BusinessException("异常操作");
        }
        appointmentMapper.change(Appointment.STATUS_CANCEL,appointment.getId());
    }

    @Override
    public void arrival(Long id) {
        Appointment appointment = this.get(id);
        if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())) {
            throw new BusinessException("异常操作");
        }
        appointmentMapper.arrival(Appointment.STATUS_ARRIVAL,appointment.getId());
    }

    @Override
    public void changeStatus(Long id) {
        appointmentMapper.change(Appointment.STATUS_SETTLE,id);
    }


}
